
/********************************************************************************************
	这是自动生成内容，请勿更改。
	如有扩展内容，请在Generate目录外新建此类进行添加。

	2014.10	Dujianyu@Autohome Inc.
********************************************************************************************/
using System;
using System.Collections.Generic;
using Read.Model;
using FluentData;

namespace Read.Data
{	
	
	/// <summary>
    /// Sources 数据库操作类
    /// </summary>
	public partial class DBSources
	{
        //Sources表内的所有字段
        internal static string _tbSourcesCols = " Id,SourceName,IndexUrl,Description ";

		
		public Sources Select(Int32 id)
		{ 
			var context = DBContext.GetContext();
            return context.Sql(" SELECT TOP 1 " + _tbSourcesCols + "FROM Sources WHERE Id = @id ")
				.Parameter("id", id)
                .QuerySingle<Sources>(); 
		}
		public IList<Sources> SelectAll()
		{
			using (var context = DBContext.GetContext())
            {
                var select = context.Select<Sources>(_tbSourcesCols)
                    .From("Sources");
                return select.QueryMany();
            }
		}
		public bool IsExist(int cid)
		{ 
			var context = DBContext.GetContext();
            return context.Sql(" SELECT COUNT(1) FROM Sources WHERE cid= "+cid)
				
                .QuerySingle<int>()>0; 
		}

		public void Update(Sources sources, IDbContext context=null)
		{
			bool hasDispose = false;
            if (context == null)
            {
                context = DBContext.GetContext();
                hasDispose = true;
            }
            try
            {
                 context.Update<Sources>("Sources", sources)
                    .AutoMap(x => x.Id)
										.Where("Id", sources.Id)
					                    .Execute();

            }
            finally
            {
                if (hasDispose)
                {
                    context.Dispose();
                }
            }
		}

		public void Insert(Sources sources , IDbContext context=null) 
        {  
            bool hasDispose = false;
            if (context == null)
            {
                context = DBContext.GetContext();
                hasDispose = true;
            }
            try
            {
                 context.Insert<Sources>("Sources", sources)
				  .AutoMap(x => x.Id)
				.Execute();
            }
            finally
            {
                if (hasDispose)
                {
                    context.Dispose();
                }
            }
        }

		public void Delete(Int32 id , IDbContext context=null) 
        {  
            bool hasDispose = false;
            if (context == null)
            {
                context = DBContext.GetContext();
                hasDispose = true;
            }
            try
            {
                 context.Sql(" DELETE FROM Sources WHERE Id = @id ")
                    .Parameter("id", id)
                    .Execute();
            }
            finally
            {
                if (hasDispose)
                {
                    context.Dispose();
                }
            }
        }
	}
	
}
	
